package joke.content;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

import org.json.JSONObject;


public class JokeApi {
	private static final String JOKE_URL = "http://www.ecook.cn/joke/viewContentByOrderServlet";

	public InputStream openConn(String path, String id) {
		InputStream is = null;
		try {
			URL realUrl = new URL(path);
			// 打开和URL之间的连接
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			PrintWriter out = new PrintWriter(new OutputStreamWriter(conn
					.getOutputStream(), "utf-8"));
			// 发送请求参数
			out.print("id=" + id);
			// flush输出流的缓冲
			out.flush();
			is = conn.getInputStream();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return is;

	}

	public JokePo getJokePo(String startId, int type) {
		JokePo po = new JokePo();
		InputStream is = openJokeConn(startId, type);
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(is,
					"UTF-8"));
			String line = in.readLine();// will refactory
			JSONObject jsonObject = new JSONObject(line);
			String content = jsonObject.getString("content");
			String id = jsonObject.getString("id");
			String rank = jsonObject.getString("rank");
			String state = jsonObject.getString("state");
			String title = jsonObject.getString("title");
			String url = jsonObject.getString("url");
			po.setContent(content);
			po.setId(id);
			po.setRank(new Integer(rank));
			po.setState(state);
			po.setTitle(title);
			po.setUrl(url);
			return po;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public InputStream openJokeConn(String startId, int type) {
		InputStream is = null;
		try {
			URL realUrl = new URL(JOKE_URL);
			// 打开和URL之间的连接
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			PrintWriter out = new PrintWriter(new OutputStreamWriter(conn
					.getOutputStream(), "utf-8"));
			// 发送请求参数
			if (type == 1) {
				out.print("id=" + startId);
			} else {
				out.print("id=" + startId + "&type=2");
			}
			// flush输出流的缓冲
			out.flush();
			is = conn.getInputStream();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return is;
	}
}
